import java.util.ArrayList;

/**
 * @author LKQ
 * @date 2021/11/25 19:06
 * @description
 */
public class Reverse {
    public static void main(String[] args) {
        System.out.println(reverse(1563847412));
        System.out.println(Math.pow(2, 31));
    }

    public static int reverse(int x) {
        ArrayList<Integer> arrayList = new ArrayList<>();
        while (x % 10 != 0 || x / 10 != 0) {
            arrayList.add(x % 10);
            x = x / 10;
        }
        Object[] a = arrayList.toArray();
        double ans = 0;
        for (int i = 0; i < a.length; i++) {
            ans = ans + (int)a[i] *  Math.pow(10, a.length-i-1);
            // 和超过
            if (ans > Math.pow(2, 31)-1 || ans < -Math.pow(2, 31)) {
                ans = 0;
                break;
            }
        }
        return (int)ans;
    }
}
